# -*- coding:utf-8 -*-
__author__ = 'LangJin'
from flask import Flask
from config import flaskConfig
from app.middleware import record_api_log,init_db,clear_db
from app.errors import errorbp
from app.notify import notify
from app.views import private,public
from app.admin import auth,views



def create_app():
    '''
    工厂方法
    '''
    app = Flask(__name__)
    app.config.from_object(flaskConfig)
    app.register_blueprint(errorbp)                                     # 注册错误蓝图
    app.register_blueprint(notify,url_prefix="/notify")                 # 注册各种回调接口
    app.register_blueprint(public,url_prefix="/langjin")                # 公共接口,不需要token
    app.register_blueprint(private,url_prefix="/langjin/auth")          # 私密接口,需要token
    app.register_blueprint(views,url_prefix="/langjin/system")          # 私密接口,需要token
    app.register_blueprint(auth,url_prefix="/langjin/system")           # 私密接口,需要token
    app.before_request(init_db)                                         # 注册中间件
    app.after_request(record_api_log)                                   # 注册中间件
    app.teardown_request(clear_db)                                      # 注册中间件
    return app
